package com.cll.pmis.config;

import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.cll.pmis.common.security.SysUserArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @author chenliangliang
 * @date 2018/6/18
 */
@Configuration
@EnableConfigurationProperties(UserFilterProperties.class)
public class AppConfig extends WebMvcConfigurerAdapter {


    private UserFilterProperties properties;

    private AntPathMatcher pathMatcher = new AntPathMatcher();

    @Autowired
    protected AppConfig(UserFilterProperties properties){
        this.properties=properties;

    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new SysUserArgumentResolver());
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        String excludePath = properties.getExclude();
//        String[] excludePaths = StringUtils.split(excludePath, ",");
//        registry.addInterceptor(new SecurityInterceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns(excludePaths)
//                .pathMatcher(pathMatcher);
//
//    }

    @Bean
    public HttpMessageConverter messageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

        FastJsonConfig config = new FastJsonConfig();
        config.setCharset(Charset.forName("UTF-8"));
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.IgnoreNonFieldGetter,
                SerializerFeature.IgnoreErrorGetter,
                SerializerFeature.SkipTransientField);
        config.setFeatures(Feature.SupportNonPublicField);

        converter.setFastJsonConfig(config);

        return converter;
    }


//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        UserFilter filter = new UserFilter(pathMatcher, properties);
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter);
//        filterRegistrationBean.addUrlPatterns("/*");
//        return filterRegistrationBean;
//    }
}
